using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TankWars
{
    public class Map
    {
        /// <summary>
        /// Image for the map
        /// </summary>
        private Texture2D MapImage;
        /// <summary>
        /// Player tank the screen is centered on
        /// </summary>
        private Tank playerTank;

        private Viewport viewPort;

        #region Properties
        /// <summary>
        /// Gets the width of the map
        /// </summary>
        public int Width
        {
            get
            {
                return this.MapImage.Width;
            }
        }

        /// <summary>
        /// Gets the height of the map
        /// </summary>
        public int Height
        {
            get
            {
                return this.MapImage.Height;
            }
        }

        /// <summary>
        /// Gets the upper-left corner of the boundary
        /// </summary>
        public Vector2 UpperBoundary
        {
            get
            {
                return new Vector2(this.viewPort.Width/2, this.viewPort.Height/2);
            }
        }

        /// <summary>
        /// Gets the lower-right corner of the boundary
        /// </summary>
        public Vector2 LowerBoundary
        {
            get
            {
                return new Vector2(this.Width - this.viewPort.Width/2, this.Height - this.viewPort.Height/2);
            }
        }
        #endregion

        /// <summary>
        /// Creates a new map that centers the given tank
        /// </summary>
        /// <param name="mapImage">Image for the map</param>
        /// <param name="playerTank">Tank to center</param>
        /// <param name="viewPort"></param>
        public Map(Texture2D mapImage, Tank playerTank, Viewport viewPort)
        {
            this.MapImage = mapImage;
            this.playerTank = playerTank;
            this.viewPort = viewPort;
        }

        /// <summary>
        /// Gets the actual screen position of the given tank for drawing purposes
        /// </summary>
        /// <param name="positionVector">The vector coordinates to get the screen position of</param>
        /// <returns>The screen position of the tank</returns>
        public Vector2 GetTrueScreenPosition(Vector2 positionVector)
        {
            //Get the difference between the map coordinates of the player tank and the given tank
            Vector2 vDiff = positionVector - this.playerTank.vPosition;
            //Get the center coordinates of the screen
            Vector2 vScreenCoordinates = new Vector2((this.viewPort.Width / 2), (this.viewPort.Height / 2));
            //The screen coordiantes of the tank is the center coordinates of the screen + the difference in the map coordinates
            vScreenCoordinates += vDiff;

            return vScreenCoordinates;
        }

        /// <summary>
        /// Draws the map, centering the screen on the player's tank
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            Vector2 vMapPosition = this.GetTrueScreenPosition(new Vector2(0.0f));
            spriteBatch.Begin();
            spriteBatch.Draw(this.MapImage, vMapPosition, Color.White);
            spriteBatch.End();
        }
    }
}
